package ${table.pag}.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jframework.core.action.BaseController;
import com.jframework.core.bean.ResultMessage;
import com.jframework.core.exception.BusinessException;
import com.jframework.core.model.DataGrid;
import com.jframework.core.model.Page;
import com.jframework.core.model.Params;

import ${table.pag}.model.${table.className} ;
import ${table.pag}.service.${table.className}Service;
@Controller
@RequestMapping("/manage/${table.className?uncap_first}")
public class ${table.className}Controller extends BaseController {
	@Autowired
	private ${table.className}Service ${table.className?uncap_first}Service;
	
	@RequestMapping("add.do")
	public ModelAndView  add(String title) {
		ModelAndView mv=this.createMV("${table.className?uncap_first}/m/${table.className?uncap_first}Add");
		${table.className} ${table.className?uncap_first} = new ${table.className}();
		mv.addObject("${table.className?uncap_first}",${table.className?uncap_first});
		return mv;
	}
	@ResponseBody
	@RequestMapping("save.do")
	public ResultMessage insert(${table.className} ${table.className?uncap_first}) {
		${table.className?uncap_first}.setId(${table.className?uncap_first}Service.getNewId());
		${table.className?uncap_first}Service.insert(${table.className?uncap_first});
		return ResultMessage.success();
	}
	
	@RequestMapping("edit.do")
	public ModelAndView edit(String id) {
		ModelAndView mv=this.createMV("${table.className?uncap_first}/m/${table.className?uncap_first}Edit");
		${table.className} ${table.className?uncap_first} = ${table.className?uncap_first}Service.getById(id);
		mv.addObject("${table.className?uncap_first}",${table.className?uncap_first});
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("update.do")
	public ResultMessage update(${table.className} ${table.className?uncap_first}) {
		${table.className} old${table.className} = ${table.className?uncap_first}Service.getById(${table.className?uncap_first}.getId());
		if(old${table.className}==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		${table.className?uncap_first}.setModifyTime(old${table.className}.getModifyTime());
		if (${table.className?uncap_first}Service.update(${table.className?uncap_first})>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}
	
	@RequestMapping("page.do")
	public ModelAndView page(${table.className} ${table.className?uncap_first}) {
		Page page = this.getPage();
		ModelAndView mv=this.createMV("${table.className?uncap_first}/m/${table.className?uncap_first}Page");
		Params params = Params.create();
		params.add(${table.className?uncap_first});
		DataGrid<${table.className}> datas=${table.className?uncap_first}Service.page(params, page);
		mv.addObject("${table.className?uncap_first}",${table.className?uncap_first});
		mv.addObject("datas",datas);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("del.do")
	public ResultMessage del(String id) {
		${table.className} ${table.className?uncap_first} = ${table.className?uncap_first}Service.getById(id);
		if(${table.className?uncap_first}==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(${table.className?uncap_first}Service.delete(${table.className?uncap_first})>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}
	
	@RequestMapping("info.do")
	public ModelAndView info(String id) {
		${table.className} ${table.className?uncap_first} = ${table.className?uncap_first}Service.getById(id);
		if(${table.className?uncap_first}==null)
			throw new BusinessException("无效的数据!");
		ModelAndView mv=this.createMV("${table.className?uncap_first}/m/${table.className?uncap_first}Info");
		mv.addObject("${table.className?uncap_first}",${table.className?uncap_first});
		return mv;
	}
}
